<?php
/*
|--------------------------------------------------------------------------
| admin_model.php
|--------------------------------------------------------------------------
| auther:Liutiansi
| Email:liutiansi@gmail.com
| update:2008-08-31
|
*/
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin_model extends Model
{
	
/*
|--------------------------------------------------------------------------
| model Initialization;
|--------------------------------------------------------------------------
|
| 
| 
|
*/		
	public $loginResult = null;
	public function Admin_model()
	{
		parent::Model();
	}


/*
|--------------------------------------------------------------------------
| return validation result info.
|--------------------------------------------------------------------------
|
| 
| 
|
*/		
	function getLoginResult()
	{
		return $this->loginResult;
	}
	
	


/*
|--------------------------------------------------------------------------
| get tables query count;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function Getcount_all($table,$Condition1,$Condition2)
	{
		if ($Condition1!=NULL)
			$this->db->or_like($Condition1);
		$this->db->where($Condition2.'!=','');	
		return $this->db->count_all_results($table);
		//show_error($this->db->last_query());
	}
	
	
/*
|--------------------------------------------------------------------------
| get table query to view;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function get_results($table,$num, $offset,$Condition1,$Condition2)
	{
		$this->db->select('*');
		if ($Condition1!=NULL)
			$this->db->or_like($Condition1);
		$this->db->where($Condition2.'!=','');
		$this->db->order_by("SERVER_APP", "desc");
		$query = $this->db->get($table, $num, $offset);
		//show_error($this->db->last_query());
		return $query;
	}



//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||DATACENTER|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

/*
|--------------------------------------------------------------------------
| get tables query count;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function Getcount_datacenter_all($Condition)
	{
		if ($Condition!=NULL)
			$this->db->or_like($Condition);	
		return $this->db->count_all_results("ty_lvs_datacenter");
		//show_error($this->db->last_query());
	}


/*
|--------------------------------------------------------------------------
| get datacenter master wip;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function get_datacenter_master($Condition)
	{
		$this->db->select('*');
		$this->db->where($Condition);
		$this->db->order_by("wip");
		$query = $this->db->get("ty_lvs_datacenter");
		return $query;
	}
	

/*
|--------------------------------------------------------------------------
| get ip for  datacenter server ip;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function get_server_datacenterip($ip)
	{
		if(!empty($ip))
		{		
			$sql="SELECT datacenter_id from ty_lvs_vip where virtual_ipaddress=(select VIP from ty_lvs_servers where serverip='$ip')";
			$query = $this->db->query($sql);
			if($query->num_rows() > 0)
			{
				$myrow=$query->row_array();
				return $myrow['datacenter_id'];
				
			}	else	return "NULL";
		}
	}


	
/*
|--------------------------------------------------------------------------
| get table query to view;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function get_datacenter_results($num, $offset,$Condition)
	{
		$this->db->select('*');
		if ($Condition!=NULL)
			$this->db->or_like($Condition);
		$this->db->order_by("gid");
		$query = $this->db->get("ty_lvs_datacenter", $num, $offset);
		return $query;
	}


/*
|--------------------------------------------------------------------------
| select datacenter edit id info;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function datacenter_select_iddb($Condition)
	{
		$this->db->where($Condition);
		$query=$this->db->get('ty_lvs_datacenter');
		if($query->num_rows() ==1 )
		{
			$myrow = $query->row_array();
			unset($query);
			return $myrow;
		}
		else
		{
			return false;
		}
	}


/*
|--------------------------------------------------------------------------
| check datacenter's IP done Exist?
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	public function _datacenter_is_unique($ip,$id)
	{	
		if(!empty($ip))
		{		
			$sql="SELECT COUNT(*) AS `numrows` from ty_lvs_datacenter where (lip='$ip' or wip='$ip')";
			if (!empty($id))
				$sql.=" and ID!=".$id;
			$query = $this->db->query($sql);
			$row=$query->row();
			return ($row->numrows==0);
		}
	}

/*
|--------------------------------------------------------------------------
| post datacenter info insert to db;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function datacenter_postdb($data)
	{
		return $this->db->insert('ty_lvs_datacenter', $data);
	}


/*
|--------------------------------------------------------------------------
| post datacenter info edit to db;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function datacenter_editdb($data,$id)
	{
		$this->db->where("ID",$id);
		return $this->db->update('ty_lvs_datacenter',$data);
	}
	


//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||VIP|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/*
|--------------------------------------------------------------------------
| get tables query count;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function Getcount_VIP_all($Condition)
	{
		if ($Condition!=NULL)
			$this->db->or_like($Condition);	
		return $this->db->count_all_results("ty_lvs_vip");
		show_error($this->db->last_query());
	}
	
	
/*
|--------------------------------------------------------------------------
| get table query to view;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function get_VIP_results($num, $offset,$Condition)
	{
		$this->db->select('*');
		if ($Condition!=NULL)
			$this->db->or_like($Condition);
		$this->db->order_by("datacenter_id");
		$query = $this->db->get("ty_lvs_vip", $num, $offset);
		return $query;
	}


/*
|--------------------------------------------------------------------------
| select VIP edit id info;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function VIP_select_iddb($Condition)
	{
		$this->db->where($Condition);
		$query=$this->db->get('ty_lvs_vip');
		if($query->num_rows() ==1 )
		{
			$myrow = $query->row_array();
			unset($query);
			return $myrow;
		}
		else
		{
			return false;
		}
	}


/*
|--------------------------------------------------------------------------
| check VIP's IP done Exist?
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	public function _VIP_is_unique($ip,$id)
	{	
		if(!empty($ip))
		{		
			$sql="SELECT COUNT(*) AS `numrows` from ty_lvs_vip where virtual_ipaddress='$ip'";
			if (!empty($id))
				$sql.=" and ID!=".$id;
			$query = $this->db->query($sql);
			$row=$query->row();
			return ($row->numrows==0);
		}
	}

/*
|--------------------------------------------------------------------------
| post VIP info insert to db;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function VIP_postdb($data)
	{
		return $this->db->insert('ty_lvs_vip', $data);
	}


/*
|--------------------------------------------------------------------------
| post VIP info edit to db;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function VIP_editdb($data,$id)
	{
		$this->db->where("ID",$id);
		return $this->db->update('ty_lvs_vip',$data);
	}
	

//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||Servers|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/*
|--------------------------------------------------------------------------
| get tables query count;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function Getcount_Servers_all($Condition)
	{
		if ($Condition!=NULL)
			$this->db->or_like($Condition);	
		return $this->db->count_all_results("ty_lvs_servers");
		show_error($this->db->last_query());
	}
	
	
/*
|--------------------------------------------------------------------------
| get table query to view;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function get_Servers_results($num, $offset,$Condition)
	{
		$this->db->select('*');
		if ($Condition!=NULL)
			$this->db->or_like($Condition);
		$this->db->order_by("VIP");
		$query = $this->db->get("ty_lvs_servers", $num, $offset);
		return $query;
	}


/*
|--------------------------------------------------------------------------
| select Servers edit id info;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function Servers_select_iddb($Condition)
	{
		$this->db->where($Condition);
		$query=$this->db->get('ty_lvs_servers');
		if($query->num_rows() ==1 )
		{
			$myrow = $query->row_array();
			unset($query);
			return $myrow;
		}
		else
		{
			return false;
		}
	}


/*
|--------------------------------------------------------------------------
| check Servers's IP done Exist?
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	public function _Servers_is_unique($ip,$id)
	{	
		if(!empty($ip))
		{		
			$sql="SELECT COUNT(*) AS `numrows` from ty_lvs_servers where (serverip='$ip' or serverlip='$ip')";
			if (!empty($id))
				$sql.=" and ID!=".$id;
			$query = $this->db->query($sql);
			$row=$query->row();
			return ($row->numrows==0);
		}
	}

/*
|--------------------------------------------------------------------------
| post Servers info insert to db;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function Servers_postdb($data)
	{
		return $this->db->insert('ty_lvs_servers', $data);
	}


/*
|--------------------------------------------------------------------------
| post Servers info edit to db;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function Servers_editdb($data,$id)
	{
		$this->db->where("ID",$id);
		return $this->db->update('ty_lvs_servers',$data);
	}
	
	
	
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||Monitors|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/*
|--------------------------------------------------------------------------
| get tables query count;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function Getcount_Monitors_all($Condition)
	{
		if ($Condition!=NULL)
			$this->db->or_like($Condition);	
		return $this->db->count_all_results("ty_lvs_monitors");
		show_error($this->db->last_query());
	}
	
	
/*
|--------------------------------------------------------------------------
| get table query to view;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function get_Monitors_results($num, $offset,$Condition)
	{
		$this->db->select('*');
		if ($Condition!=NULL)
			$this->db->or_like($Condition);
		$this->db->order_by("monitors_name");
		$query = $this->db->get("ty_lvs_monitors", $num, $offset);
		return $query;
	}


/*
|--------------------------------------------------------------------------
| select Monitors edit id info;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function Monitors_select_iddb($Condition)
	{
		$this->db->where($Condition);
		$query=$this->db->get('ty_lvs_monitors');
		if($query->num_rows() ==1 )
		{
			$myrow = $query->row_array();
			unset($query);
			return $myrow;
		}
		else
		{
			return false;
		}
	}


/*
|--------------------------------------------------------------------------
| check Monitors's IP done Exist?
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	public function _Monitors_is_unique($ip,$id)
	{	
		if(!empty($ip))
		{		
			$sql="SELECT COUNT(*) AS `numrows` from ty_lvs_monitors where monitors_name='$ip'";
			if (!empty($id))
				$sql.=" and ID!=".$id;
			$query = $this->db->query($sql);
			$row=$query->row();
			return ($row->numrows==0);
		}
	}

/*
|--------------------------------------------------------------------------
| post Monitors info insert to db;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function Monitors_postdb($data)
	{
		return $this->db->insert('ty_lvs_monitors', $data);
	}


/*
|--------------------------------------------------------------------------
| post Monitors info edit to db;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function Monitors_editdb($data,$id)
	{
		$this->db->where("ID",$id);
		return $this->db->update('ty_lvs_monitors',$data);
	}
	
/*
|--------------------------------------------------------------------------
| check Monitors's name done Exist in ty_lvs_server?
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	public function _Monitors_is_server_unique($Condition)
	{	
		if ($Condition!=NULL)
			$sql="";
			foreach($Condition as $key=>$value)
			{
				$sql.="id=".$value." or ";
			}
			$sql=substr($sql,0,-4);
		$sql_str="SELECT COUNT(*) AS `numrows` FROM ty_lvs_servers Where Monitors in (Select monitors_name  FROM ty_lvs_monitors Where ".$sql.")";
		$query = $this->db->query($sql_str);
		$row=$query->row();
		return ($row->numrows==0);
	}
	
	






	
/*
|--------------------------------------------------------------------------
| get get_lvsip_results to list;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function get_lvsip_results()
	{
		$this->db->select('wip');
		$this->db->where("state","MASTER");
		$this->db->order_by("wip");
		$query = $this->db->get("ty_lvs_datacenter");
		return $query;
	}	
	
/*
|--------------------------------------------------------------------------
| get get_vipip_results to list;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function get_vipip_results()
	{
		$this->db->select('virtual_ipaddress');
		$this->db->order_by("virtual_ipaddress");
		$query = $this->db->get("ty_lvs_vip");
		return $query;
	}

	
/*
|--------------------------------------------------------------------------
| get_monitors_par results to list;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function get_monitors_par()
	{
		$this->db->select('monitors_name');
		$this->db->order_by("monitors_name");
		$query = $this->db->get("ty_lvs_monitors");
		return $query;
	}

	
/*
|--------------------------------------------------------------------------
| check focus's IP done Exist?
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	public function _is_unique($ip,$id)
	{	
		if(!empty($ip))
		{		
			$sql="SELECT COUNT(*) AS `numrows` from ty_server_focus where (SERVER='$ip' or SERVER_LIP='$ip')";
			if (!empty($id))
				$sql.=" and ID!=".$id;
			$query = $this->db->query($sql);
			$row=$query->row();
			return ($row->numrows==0);
		}
	}
	

/*
|--------------------------------------------------------------------------
| post focus info insert to db;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function focus_postdb($data)
	{
		return $this->db->insert('ty_server_focus', $data);
	}
	

/*
|--------------------------------------------------------------------------
| post opt logs into insert to db;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function logs_postdb($data)
	{
		return $this->db->insert('user_logs', $data);
	}



/*
|--------------------------------------------------------------------------
| post focus info edit to db;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function focus_editdb($data,$id)
	{
		$this->db->where("ID",$id);
		return $this->db->update('ty_server_focus',$data);
	}


/*
|--------------------------------------------------------------------------
| select focus edit id info;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function focus_select_iddb($Condition)
	{
		$this->db->where($Condition);
		$query=$this->db->get('ty_server_focus');
		if($query->num_rows() ==1 )
		{
			$myrow = $query->row_array();
			unset($query);
			return $myrow;
		}
		else
		{
			return false;
		}
	}
	
	
/*
|--------------------------------------------------------------------------
| delete tables data;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function set_delete($table,$Condition)
	{
		if ($Condition!=NULL)
			$sql="";
			foreach($Condition as $key=>$value)
			{
				$sql.="id=".$value." or ";
			}
			$sql=substr($sql,0,-4);
		$sql="DELETE FROM `".$table."` WHERE ".$sql;
		return $this->db->query($sql);
	}	


/*
|--------------------------------------------------------------------------
| update tables data;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function set_update($table,$Condition,$Field,$Value)
	{
		if ($Condition!=NULL)
			$sql="";
			foreach($Condition as $key=>$value)
			{
				$sql.="id=".$value." or ";
			}
			$sql=substr($sql,0,-4);
		$sql="UPDATE `".$table."` set ".$Field."='".$Value."' WHERE ".$sql;
		return $this->db->query($sql);
	}



/*
|--------------------------------------------------------------------------
| Production config's for keepslived.
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	public function ProductionConfig($lvsip,$status)
	{	
		$ConfigStr="! Configuration File for keepalived\n";
		$ConfigStr.="global_defs {\n";
		$ConfigStr.="\tnotification_email \n";
		$ConfigStr.="\t{\n";
		if(!empty($lvsip))
		{		
			#lvs global config
			$sql_lvs="SELECT * from ty_lvs_datacenter where wip='$lvsip'";
			$query_lvs = $this->db->query($sql_lvs);
			$row_lvs=$query_lvs->row();
			$ConfigStr.="\t\t".$row_lvs->notification_email."\n";
			$ConfigStr.="\t}\n";
			$ConfigStr.="\tnotification_email_from ".$row_lvs->notification_email_from."\n";
			$ConfigStr.="\tsmtp_server ".$row_lvs->smtp_server."\n";
			$ConfigStr.="\tsmtp_connect_timeout ".$row_lvs->smtp_connect_timeout."\n";
			$ConfigStr.="\trouter_id ".$row_lvs->router_id."\n";
			$ConfigStr.="}\n\n";
			$ConfigStr.="vrrp_instance VI_1 {\n";
			if ($status=='m')
				$ConfigStr.="\tstate ".$row_lvs->state."\n";
			else
				$ConfigStr.="\tstate BACKUP\n";
			$ConfigStr.="\tinterface ".$row_lvs->interface."\n";
			$ConfigStr.="\tlvs_sync_daemon_inteface ".$row_lvs->lvs_sync_daemon_inteface."\n";
			$ConfigStr.="\tvirtual_router_id ".$row_lvs->virtual_router_id."\n";
			if ($status=='m')
				$ConfigStr.="\tpriority ".$row_lvs->priority."\n";
			else
				$ConfigStr.="\tpriority ".($row_lvs->priority-1)."\n";
			$ConfigStr.="\tadvert_int ".$row_lvs->advert_int."\n";
			$ConfigStr.="\tauthentication {\n";
			$ConfigStr.="\tauth_type ".$row_lvs->auth_type."\n";
			$ConfigStr.="\tauth_pass ".$row_lvs->auth_pass."\n";
			$ConfigStr.="\t}\n";
			$ConfigStr.="\tvirtual_ipaddress {\n";

			#vip ip
			$sql_vip_ip="SELECT virtual_ipaddress from ty_lvs_vip where datacenter_id='$lvsip'";
			$query_vip_ip = $this->db->query($sql_vip_ip);
			foreach($query_vip_ip->result() as $myrow_vip_ip)
			{
				$ConfigStr.="\t\t".$myrow_vip_ip->virtual_ipaddress."\n";
			}
			$ConfigStr.="\t}\n";
			$ConfigStr.="}\n\n";
			
			#vip
			$sql_vip="SELECT * from ty_lvs_vip where datacenter_id='$lvsip'";
			$query_vip = $this->db->query($sql_vip);
			foreach($query_vip->result() as $myrow_vip)
			{
				$ConfigStr.="virtual_server ".$myrow_vip->virtual_ipaddress." ".$myrow_vip->port." {\n";
				$ConfigStr.="\tdelay_loop ".$myrow_vip->delay_loop."\n";
				$ConfigStr.="\tlb_algo  ".$myrow_vip->lb_algo ."\n";
				$ConfigStr.="\tlb_kind ".$myrow_vip->lb_kind."\n";
				$ConfigStr.="\tpersistence_timeout ".$myrow_vip->persistence_timeout."\n";
				$ConfigStr.="\tprotocol ".$myrow_vip->protocol."\n\n";
				
				#server
				$sql_server="SELECT * from ty_lvs_servers where VIP='$myrow_vip->virtual_ipaddress'";
				$query_server = $this->db->query($sql_server);
				foreach($query_server->result() as $myrow_server)
				{
					$ConfigStr.="\treal_server ".$myrow_server->serverip." ".$myrow_server->port." {\n";
					$ConfigStr.="\t\tweight ".$myrow_server->weight."\n";
			
					#monitor
					$sql_monitor="SELECT * from ty_lvs_monitors where monitors_name='$myrow_server->Monitors'";
					$query_monitor = $this->db->query($sql_monitor);
					$row_monitor=$query_monitor->row();
					switch ($row_monitor->monitors_type)
					{
						case 'HTTP_GET':
							$ConfigStr.="\t\tHTTP_GET {\n";
							$ConfigStr.="\t\t\turl {\n";
							$ConfigStr.="\t\t\t\tpath ".$row_monitor->monitors_par1."\n";
							$ConfigStr.="\t\t\t\tdigest ".$row_monitor->monitors_par2."\n";
							$ConfigStr.="\t\t\t}\n";
							$ConfigStr.="\t\t\tconnect_timeout 10\n";
							$ConfigStr.="\t\t\tnb_get_retry 3\n";
							$ConfigStr.="\t\t\tdelay_before_retry 3\n";
							$ConfigStr.="\t\t\tconnect_port ".$myrow_server->port."\n";
							$ConfigStr.="\t\t}\n";
							break;
						case 'SSL_GET':
							$ConfigStr.="\t\tSSL_GET {\n";
							$ConfigStr.="\t\t\turl {\n";
							$ConfigStr.="\t\t\t\tpath ".$row_monitor->monitors_par1."\n";
							$ConfigStr.="\t\t\t\tdigest ".$row_monitor->monitors_par2."\n";
							$ConfigStr.="\t\t\t}\n";
							$ConfigStr.="\t\t\tconnect_timeout 10\n";
							$ConfigStr.="\t\t\tnb_get_retry 3\n";
							$ConfigStr.="\t\t\tdelay_before_retry 3\n";
							$ConfigStr.="\t\t\tconnect_port ".$myrow_server->port."\n";
							$ConfigStr.="\t\t}\n";
							break;
						case 'TCP_CHECK':
							$ConfigStr.="\t\tTCP_CHECK {\n";
							$ConfigStr.="\t\t\tconnect_port ".$row_monitor->monitors_par1."\n";
							$ConfigStr.="\t\t\tconnect_timeout ".$row_monitor->monitors_par2."\n";
							$ConfigStr.="\t\t\tnb_get_retry 3\n";
							$ConfigStr.="\t\t\tdelay_before_retry 3\n";
							$ConfigStr.="\t\t}\n";
							break;
						case 'SMTP_CHECK':
							$ConfigStr.="\t\tSMTP_CHECK {\n";
							$ConfigStr.="\t\t\thost {\n";
							$ConfigStr.="\t\t\t\tconnect_port ".$row_monitor->monitors_par1."\n";
							$ConfigStr.="\t\t\t}\n";
							$ConfigStr.="\t\t\tconnect_timeout ".$row_monitor->monitors_par2."\n";
							$ConfigStr.="\t\t\tretry 3\n";
							$ConfigStr.="\t\t\tdelay_before_retry 3\n";
							$ConfigStr.="\t\t\tconnect_port ".$myrow_server->port."\n";
							$ConfigStr.="\t\t}\n";
							break;
						case 'MISC_CHECK':
							$ConfigStr.="\t\tMISC_CHECK {\n";
							$ConfigStr.="\t\t\tmisc_path ".$row_monitor->monitors_par1."\n";
							$ConfigStr.="\t\t\tmisc_timeout ".$row_monitor->monitors_par2."\n";
							$ConfigStr.="\t\t\tmisc_dynamic\n";
							$ConfigStr.="\t\t}\n";
							break;
					}
					$ConfigStr.="\t}\n";
				}
				$ConfigStr.="}\n\n";
			}
		}
		return $ConfigStr;
	}



/*
|--------------------------------------------------------------------------
| return LVS BACKUP IP;
|--------------------------------------------------------------------------
|
| $ip=wip(MASTER IP)
| 
|
*/	
	function get_BACKUP_IP($ip)
	{
		$sql_str="SELECT wip FROM ty_lvs_datacenter Where gid= (Select gid  FROM ty_lvs_datacenter Where wip='$ip') and state='BACKUP'";
		$query = $this->db->query($sql_str);
		if($query->num_rows() > 0)
		{
			$myrow=$query->row_array();
			return $myrow['wip'];
			
		}	else	return "NULL";
	}


/*
|--------------------------------------------------------------------------
| return LVS MASTER IP;
|--------------------------------------------------------------------------
|
| $ip=wip(BACKUP IP)
| 
|
*/	
	function get_MASTER_IP($ip)
	{
		$sql_str="SELECT wip FROM ty_lvs_datacenter Where gid= (Select gid FROM ty_lvs_datacenter Where wip='$ip') and state='MASTER'";
		$query = $this->db->query($sql_str);
		if($query->num_rows() > 0)
		{
			$myrow=$query->row_array();
			return $myrow['wip'];
			
		}	else	return "NULL";
	}
	
/*
|--------------------------------------------------------------------------
| get datacenter hosts;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function get_datacenter_hosts()
	{
		$this->db->select('servername,lip');
		$this->db->order_by("ID");
		$query = $this->db->get("ty_lvs_datacenter");
		return $query;
	}
	

/*
|--------------------------------------------------------------------------
| get server hosts;
|--------------------------------------------------------------------------
|
| 
| 
|
*/	
	function get_server_hosts()
	{
		$this->db->select('servername,serverlip');
		$this->db->order_by("ID");
		$query = $this->db->get("ty_lvs_servers");
		return $query;
	}
}